package com.honvay.cola.cloud.uc;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;


/**
 * Created by Yanwu 2018/2/8.
 */
public class JsonToSqlTest {
    private static final String PATH = "E:\\aibeen_coods\\cola-cloud\\cola-cloud-platform\\cola-cloud-uc\\cola-cloud-uc-service\\src\\main\\java\\com\\honvay\\cola\\cloud\\uc\\pcacode.json";
    private static final String PATH1 = "E:\\aibeen_coods\\cola-cloud\\cola-cloud-platform\\cola-cloud-uc\\cola-cloud-uc-service\\src\\main\\java\\com\\honvay\\cola\\cloud\\uc\\pcacode.sql";

    public static void main(String[] args) throws Exception {
        System.out.println("========== JSON ---> 转换成 SQL 开始 ==========");
        jsonToNation();
        jsonToProvince();
        jsonToCity();
        System.out.println("========== JSON ---> 转换成 SQL 结束 ==========");
    }

    private static void jsonToNation() throws Exception {
        String sqlStr = "insert into `s_nation` (`id`, `name`, `code`) values (" +1 + ", " + "\"中国\"" + " , " + "\"86\"); \r\n";
        System.out.println(sqlStr);
        File file = new File(PATH1);
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fileWriter = new FileWriter(file, true);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(sqlStr);
        bufferedWriter.close();
    }

    private static void jsonToProvince() throws Exception {
        JsonParser jsonParser = new JsonParser();
        JsonArray jsonArray = (JsonArray) jsonParser.parse(new FileReader(PATH));
        for (int i = 0; i < jsonArray.size(); i++) {
            JsonElement jsonElement = jsonArray.get(i);
            JsonObject featuresObj = jsonElement.getAsJsonObject();
            String name = featuresObj.get("name").toString();
            String code = featuresObj.get("code").toString();
            String sqlStr = "insert into `s_province` (`id`, `name`, `code`, `fk_nation_code`, `fk_nation_id`) values (" + (i + 1) + ", " + name + " , " + code + " , " + "\"86\" , null ); \r\n";
            System.out.println(sqlStr);
            File file = new File(PATH1);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(file, true);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(sqlStr);
            bufferedWriter.close();

        }

    }


    private static int pId = 0;

    private static void jsonToCity() throws Exception {
        JsonParser jsonParser = new JsonParser();
        JsonArray jsonArray = (JsonArray) jsonParser.parse(new FileReader(PATH));
        for (int i = 0; i < jsonArray.size(); i++) {
            JsonElement jsonElement = jsonArray.get(i);
            JsonObject featuresObj = jsonElement.getAsJsonObject();
            String code1 = featuresObj.get("code").toString();
            JsonElement childs = featuresObj.get("childs");
            JsonArray asJsonArray = childs.getAsJsonArray();

            for (int j = 0; j < asJsonArray.size(); j++) {
                JsonElement jsonElement1 = asJsonArray.get(j);
                JsonObject featuresObj1 = jsonElement1.getAsJsonObject();
                String name = featuresObj1.get("name").toString();
                String code = featuresObj1.get("code").toString();
                pId++;
                String sqlStr = "insert into `s_city` (`id`, `name`, `code`, `fk_province_code`, `fk_province_id`) values (" + pId + ", " + name + " , " + code + " , " + code1 + " , " + "null ); \r\n";
                System.out.println(sqlStr);
                File file = new File(PATH1);
                if (!file.exists()) {
                    file.createNewFile();
                }
                FileWriter fileWriter = new FileWriter(file, true);
                BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                bufferedWriter.write(sqlStr);
                bufferedWriter.close();
            }

        }

    }

//    private static void jsonToExcel() throws Exception {
//
//        JsonElement features = jsonObject.get("features");
//        JsonArray asJsonArray = features.getAsJsonArray();
//        for (int i = 0; i < asJsonArray.size(); i++) {
//            JsonElement jsonElement = asJsonArray.get(i);
//            JsonObject featuresObj = jsonElement.getAsJsonObject();
//            JsonElement attributes = featuresObj.get("attributes");
//            JsonObject attributesObj = attributes.getAsJsonObject();
//            JsonElement swchName = attributesObj.get("swchName");
//            String nameStr = swchName.toString();
//            JsonElement geometry = featuresObj.get("geometry");
//            JsonObject geometryObj = geometry.getAsJsonObject();
//            JsonElement path = geometryObj.get("paths");
//            String pathStr = path.toString();
//            String replace = nameStr.replace("\"", "");
//            String sqlStr = "insert into poly_line (id, box_name, paths) values (" + i + ", '" + replace + "', '" + pathStr + "'); \r\n";
//            System.out.println(sqlStr);
//            File file = new File("./pcacode.sql");
//            if (!file.exists()) {
//                file.createNewFile();
//            }
//            FileWriter fileWriter = new FileWriter(file, true);
//            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
//            bufferedWriter.write(sqlStr);
//            bufferedWriter.close();
//        }
//    }
}